
# try:
#     print("code started")
#     # raise KeyError
#     # raise IndexError
# except KeyError as e:
#     print("key error")
# else:
#     print("other error")
# finally:
#     print("finally")

# 上下文管理器协议
class Sample:
    def __enter__(self):
        # 获取资源
        print("enter")
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        # 释放资源
        print("exit")

    def do_something(self):
        print("doing something")

with Sample() as sample:
    sample.do_something()

